<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
//事件接受事件相关模型实例，广播事件
class Event implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;
    public $id;
    public $txt;
    public $dialogueId;
    public $room_id;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user,$txt,$dialogueId,$room_id)
    {
        $this->user=$user;
        $this->txt=$txt;
        $this->dialogueId=$dialogueId;
        $this->room_id=$room_id;
    }

    /**
     * 指定广播数据
     *
     * @return array
     */
    public function broadcastWith()
    {
        $url = asset('uploads/'.$this->user->headpic);
        $time = date('h:i');
        /*if($this->dialogueId==-2){
            $ids=$this->txt->pluck('id');
            $names = $this->txt->pluck('name');
            $this->txt=[$ids,$names];
        }*/
        return ['user' => $this->user,'txt'=>$this->txt,'dialogueId'=>$this->dialogueId,'roomId'=>$this->room_id,'url'=>$url,'time'=>$time];
    }
    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PresenceChannel('chat.'.$this->room_id);
        //return ['laravel-broadcast-channel'];
    }
}
